<template>
        <view class="background" style="position: relative; left: 43rpx; top: 61rpx">
            <image class="detailsImg" :src=searchResult[0].pic>
            <text class="detailsName" style="color: red;">{{searchResult[0].name}}</text>
            <button class="detailsForm" @click="detailsFood">营养成分</button>
			<view class="detailsData">
                约
                <input class="inputData" type="number" placeholder="请输入数字" maxlength="8"  @input="setMuch" />
                g
			<view>
			  <uni-data-select v-model="value" :localdata="range" placeholder="请选择时间" class="selectTime"></uni-data-select>
			</view>
            </view>
            <button class="cardButton"  @tap="addTodayCard">添加到今日饮食打卡</button>
        </view>
</template>

<script>
export default {
    data() {
        return {
            range: [{value:0,text:"早餐"},{value:1,text:"中餐"},{value:2,text:"晚餐"},{value:3,text:"其它"},],
			value: -1,
			foodKGnum: 0,
			searchResult:[],
			searchId:0,
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
		this.searchId=options.id
		var urlstr = 'foodInfo/get/id=' + this.searchId
		this.$https({
			url: this.$baseUrl + urlstr,
			success: (res) => {
				if (res.statusCode == 200) {
						this.searchResult.push({
							'category': res.data['data'].category,
							'cellulose': res.data['data'].cellulose,
							'energy': res.data['data'].energy,
							'fat': res.data['data'].fat,
							'id': res.data['data'].id,
							'name': res.data['data'].name,
							'pic': res.data['data'].pic,
							'protein': res.data['data'].protein,
							'state': res.data['data'].state,
							'sugar': res.data['data'].sugar,
							'version': res.data['data'].version,
					})
				} else {
					uni.showToast({
						title: res.data.errorCode,
						icon: 'error',
					})
				}
			},
		
			fail: (e) => {
				console.log(e)
				uni.showToast({
					title: '搜索失败',
					icon: 'error',
				})
			}
		});
	},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        addTodayCard: function () {
            var num = this.value;
			var KG=this.foodKGnum;
			var pages=getCurrentPages();
			var indexUrl='pages/health/food/clockIn/clockIn';
			var delta=1;
			for(var i=1;i<pages.length;i++)
			{
				if(indexUrl==pages[pages.length-i].route)
				{
					delta=i-1
				}
			};
			getApp().globalData.foodNum=num
			if(this.foodKGnum==0)
			{
				uni.showToast({
					title: '重量不能为0',
					icon: 'error',
				})
				return
			}
			if(num==-1)
			{
				uni.showToast({
					title: '未选择时间',
					icon: 'error',
				})
				return
			}
			getApp().globalData.foodKG=KG
			getApp().globalData.foodName=this.searchResult[0].name
			getApp().globalData.foodId=this.searchId
			console.log(getApp().globalData.foodKG)
			if(delta!=1)
			{
				uni.navigateBack({
					delta:delta
				});
			}
			else
			{
				uni.navigateTo({
					url:"/pages/health/food/clockIn/clockIn"
				})
			}
        },
		setMuch: function(e){
			this.foodKGnum=e.detail.value
		},
		detailsFood: function(){
			uni.navigateTo({
				url:"/pages/health/food/details/composition/composition?id="+this.searchId
			})
		}
    }	
};
</script>
<style>
@import './details.css';
</style>
